package com.yc.nfc;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author benjaminwan
 *         数据转换工具
 */
public class MyFunc {
    //-------------------------------------------------------
    // 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
    static public int isOdd(int num) {
        return num & 0x1;
    }

    //-------------------------------------------------------
    static public int HexToInt(String inHex)//Hex字符串转int
    {
        return Integer.parseInt(inHex, 16);
    }

    //-------------------------------------------------------
    static public byte HexToByte(String inHex)//Hex字符串转byte
    {
        return (byte) Integer.parseInt(inHex, 16);
    }

    //-------------------------------------------------------
    static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
    {
        return String.format("%02x", inByte).toUpperCase();
    }

    //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr)//字节数组转转hex字符串
    {
        StringBuilder strBuilder = new StringBuilder();
        int j = inBytArr.length;
        for (int i = 0; i < j; i++) {

            strBuilder.append(Byte2Hex(inBytArr[i]));
            strBuilder.append(" ");
        }
        return strBuilder.toString();
    }
  static final String TAG="T1";

    static public List<TLV> ByteArrToHexBB(byte[] arrss) {
        List<TLV> mTlvs =new ArrayList<TLV>();
        Log.d(TAG, "arrssleng" + arrss.length);
        int dataLen = 0;
        if (arrss != null) {
            //
            int end = arrss.length - 4;

            if (arrss.length > 7) {
                String resData = ByteArrToHex(arrss);
                if (resData.contains("5A")) {
                    dataLen = arrss[2];

                    //CRC效验
                    byte[] arrCrc = subBytes(arrss, 2, end);
                    int CRC = 0;
                    for (int i = 0; i < arrCrc.length - 1; i++) {
                        CRC ^= arrCrc[i];
                    }
                    CRC = ~CRC;//按位取反
                    Log.d(TAG, "CRC" + CRC);
                    int reCRC = arrss[arrss.length-3];
                    Log.d(TAG, "reCRC" + reCRC);
                    if (CRC == reCRC) {
                        Log.d(TAG, "CRC" + "效验成功");
                    } else {
                        Log.d(TAG, "CRC" + "效验失败");
                        return null;
                    }
                    //

                } else {
                    return null;
                }

            }



        byte[] arr = subBytes(arrss, 4, end);

        Log.d(TAG, "dataLen=" + dataLen);
        Log.d(TAG, "end=" + end);

            TLV mTLV;
        for (int i = 0; i < dataLen; i++) {
             mTLV =new TLV();

            StringBuilder sb = new StringBuilder();
            byte[] arrA = subBytes(arr, i, (arr.length - i));
            int tag = ntohs(arrA);
            Log.d(TAG, "tag=" + tag + " 第 i =" + i);
            mTLV.tag =tag;
            byte[] arr1 = subBytes(arr, i + 2, (arr.length - (i + 2)));
            int len = ntohs(arr1);
            Log.d(TAG, "len=" + len);
            mTLV.len = len;
            byte[] arr2 = subBytes(arr, i + len, (arr.length - (i + len)));

            String value;
            if (i == 0) {//第一次 len 位置
                arr2 = subBytes(arr, i + 2 + len, (arr.length - (i + 2 + len)));
                value = ntohsInt(arr2)+"";
                Log.d(TAG, "value0=" + value);

            } else {
                byte[] cardId = new byte[4];
                cardId[0] = arr2[3];
                cardId[1] = arr2[2];
                cardId[2] = arr2[1];
                cardId[3] = arr2[0];
                for (int n = 0; n < cardId.length; n++) {

                  /*  byte[] bytes =  new byte[]{arr2[n]};
                    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
                    int result = in.read();
                    value =result;
                    sb.append(value);
                    sb.append("|");*/

                    sb.append(Byte2Hex(cardId[n]));
                }
                Log.d(TAG, "value=" + sb.toString());
                value = sb.toString();
            }
            mTLV.value =value;

            i += (len + 3);
            mTlvs.add(mTLV);
//            i = i+3 +len;
        }
        }

        return mTlvs;
    }

    static public String ByteArrToHexAA(byte[] inBytArr)//字节数组转hex字符串
    {
        byte[] arr = subBytes(inBytArr, 4, 16);

        int Tag = ntohs(arr);
        Log.d("TTTT", "Tag=" + Tag);
        byte[] arr1 = subBytes(arr, 2, 12);
        int len = ntohs(arr1);
        Log.d("TTTT", "len=" + len);

        byte[] arr2 = subBytes(arr1, 1, 10);
        int value = ntohs(arr2);
        Log.d("TTTT", "value=" + value);
        //
        int endLen = 0;


        //

        byte[] arr3 = subBytes(arr2, 2, 8);
        int tag1 = ntohs(arr3);
        Log.d("TTTT", "tag1=" + tag1);

        byte[] arr4 = subBytes(arr3, 2, 6);
        int len1 = ntohs(arr4);
        Log.d("TTTT", "len1=" + len1);

        byte[] arr5 = subBytes(arr4, 2, 4);
        int value1 = ntohs(arr5);
        Log.d("TTTT", "value1=" + value1);





      /*  byte[] arr = subBytes(inBytArr, 4, 16);
        Log.d("TTTT","length -"+arr.length);
        byte[] arr5 = subBytes(arr, 8, 10);
        int value1 = ntohs(arr5);
        Log.d("TTTT", "value1=" + value1);*/
//		byte[] arr4 = subBytes(arr3,1,9);
//		int len1 = ntohs(arr4);
//		Log.d("TTTT","len1="+ len1);
//		int j=inBytArr.length;
//        for(int i = 0; i < j; i++){
//
//		}

	/*	int Tag = ntohs(arr);
        int len = ntohs(arr+2);
		Log.d("TTTT",Tag+"");
		Log.d("TTTT",len+"");*/
      /*  byte[] arr = subBytes(inBytArr, 4, 16);

        for (int i = 0; i < arr.length; i++) {
            int Tag11 = ntohs(arr);
            Log.d("KKKK","tag="+Tag11);
          //  int Len11 = ntohs(inBytArr);
            i += (i + 4);
        }*/
        return "";
    }

    static int ntohs(byte[] arr) {
        return ((arr[1] << 8 & 0xff00) | arr[0]);
    }

    static int ntohsIntLen(byte[] arr) {
        return (arr[0]);
    }

    static int ntohsInt(byte[] arr) {
        return (arr[0]);
    }

    public static byte[] subBytes1(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }

    /**
     * 从一个byte[]数组中截取一部分
     *
     * @param src
     * @param begin
     * @param count
     * @return
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i = begin; i < begin + count; i++)
            bs[i - begin] = src[i];
        return bs;
    }

    //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount)//字节数组转转hex字符串，可选长度
    {
        StringBuilder strBuilder = new StringBuilder();
        int j = byteCount;
        for (int i = offset; i < j; i++) {
            strBuilder.append(Byte2Hex(inBytArr[i]));
        }
        return strBuilder.toString();
    }

    //-------------------------------------------------------
    //转hex字符串转字节数组
    static public byte[] HexToByteArr(String inHex)//hex字符串转字节数组
    {
        int hexlen = inHex.length();
        byte[] result;
        if (isOdd(hexlen) == 1) {//奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {//偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = HexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
}